/**
 * Created by LUOLIANG on 2016/6/17.
 * 购物清单页面js文件
 */
var shoppinglist = avalon.define({
    $id: "shoppinglistController",
    todo: 0,
    finish: 0,
    todolist: "",
    finishlist: "",

    loadlist: function () {
        $.ajax({
            type: "post",
            url: "/CounterDrops/Shopping/shoppinglist",
            data: {},
            success: function (data) {
                var allList = data.model;
                shoppinglist.todolist = allList.todolist;
                shoppinglist.finishlist = allList.finishlist;
                shoppinglist.todo = shoppinglist.todolist.length;
                shoppinglist.finish = shoppinglist.finishlist.length;
            }
        });
    },
    Finish: function (id, index) {
        var li = $("li:eq(" + index + ")").get(0);
        $("ul li:eq(" + index + ")").remove();
        $.ajax({
            type: "post",
            url: "/CounterDrops/Shopping/updateShopping",
            data: {
                id: id
            },
            success: function (data) {
                if (data.success == true) {
                    toastr.warning("已完成");
                    mui.swipeoutClose(li);
                    shoppinglist.loadlist();
                } else {
                    toastr.warning(data.msg);
                }
            }
        });
    },

    del: function (id, index) {
        var li = $("li:eq(" + index + ")").get(0);
        $("ul li:eq(" + index + ")").remove();

        $.ajax({
            type: "post",
            url: "/CounterDrops/Shopping/delShopping",
            data: {
                id: id
            },
            success: function (data) {
                if (data.success == true) {
                    toastr.warning(data.msg);
                    mui.swipeoutClose(li);
                    shoppinglist.loadlist();
                } else {
                    toastr.warning("删除失败");
                }
            }
        });
    },

    add: function (content) {
        $.ajax({
            type: "post",
            url: "/CounterDrops/Shopping/addShopping",
            data: {
                content: content
            },
            success: function (data) {
                if (data.success == true) {
                    toastr.warning(data.msg);
                    shoppinglist.loadlist();
                } else {
                    toastr.warning("添加失败");
                }
            }
        });
    }
});
shoppinglist.loadlist();
